import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Register from '../views/Register.vue'
import Layout from '../views/Layout.vue'
import Dashboard from '../views/Dashboard.vue'
import BillList from '../views/BillList.vue'
import BankCard from '../views/BankCard.vue'
import WishList from '../views/WishList.vue'
import Memo from '../views/Memo.vue'
import Announcement from '../views/Announcement.vue'
import Profile from '../views/Profile.vue'
import Schedule from '../views/Schedule.vue'
import store from '../store'

Vue.use(VueRouter)

const routes = [
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    {
        path: '/register',
        name: 'Register',
        component: Register
    },
    {
        path: '/',
        component: Layout,
        redirect: '/dashboard',
        children: [
            {
                path: 'dashboard',
                name: 'Dashboard',
                component: Dashboard
            },
            {
                path: 'bill',
                name: 'BillList',
                component: BillList
            },
            {
                path: 'bank-card',
                name: 'BankCard',
                component: BankCard
            },
            {
                path: 'wish-list',
                name: 'WishList',
                component: WishList
            },
            {
                path: 'memo',
                name: 'Memo',
                component: Memo
            },
            {
                path: 'announcement',
                name: 'Announcement',
                component: Announcement
            },
            {
                path: 'profile',
                name: 'Profile',
                component: Profile
            },
            {
                path: 'schedule',
                name: 'Schedule',
                component: Schedule,
                meta: { title: '日程管理', icon: 'calendar' }
            }
        ]
    },
    {
        path: '/password',
        component: Layout,
        children: [
            {
                path: 'index',
                name: 'Password',
                component: () => import('@/views/password/index'),
                meta: { title: '密码管理', icon: 'lock' }
            }
        ]
    },
    {
        path: '/site',
        component: Layout,
        children: [
            {
                path: 'index',
                name: 'Site',
                component: () => import('@/views/site/index'),
                meta: { title: '站点管理', icon: 'link' }
            },
            {
                path: 'category',
                name: 'SiteCategory',
                component: () => import('@/views/site/category'),
                meta: { title: '站点分类', icon: 'folder' }
            }
        ]
    },
    {
        path: '/blog',
        component: Layout,
        children: [
            {
                path: 'index',
                name: 'BlogArticle',
                component: () => import('@/views/blog/index'),
                meta: { title: '博客文章', icon: 'document' }
            },
            {
                path: 'category',
                name: 'BlogCategory',
                component: () => import('@/views/blog/category'),
                meta: { title: '博客分类', icon: 'folder' }
            }
        ]
    },
    {
        path: '/admin',
        component: Layout,
        children: [
            {
                path: 'dashboard',
                name: 'AdminDashboard',
                component: () => import('../views/dashboard/AdminDashboard.vue'),
                meta: { title: '管理员首页', icon: 'dashboard', requiresAdmin: true }
            }
        ]
    }
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
    const token = localStorage.getItem('token')
    const isAdmin = store.getters.isAdmin

    if (to.matched.some(record => record.meta.requiresAdmin)) {
        if (token && isAdmin) {
            next()
        } else if (token && !isAdmin) {
            next('/dashboard')
        } else {
            next('/login')
        }
    } else {
        if (to.path !== '/login' && to.path !== '/register' && !token) {
            next('/login')
        } else {
            next()
        }
    }
})

export default router 